<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <517577550@qq.com>
// +----------------------------------------------------------------------
/**
 * 主题Model
 * @author xinqiyang
 * @date   2010-7-8
 *
 */
class TopicModel extends MetaModel
{


	protected $_validate  = array(
	array ('title','require','话题标题不能为空',1,'',1),
	array ('group_id','require','群组不能为空',1,'',1),
	array ('content','require','内容不能为空',1,'',3),
	);


	protected $_auto = array(
	array ('createtime','time',1,'function'),
	array ('lastreplytime','time',1,'function'),
	array ('user_id','userID',1,'function'),
	array ('status','1'),
		

	);
	
	public function getTopic($id)
	{
		return $this->query("
		SELECT sz_topic.id,sz_topic.title,sz_topic.tags,sz_topic.desc,sz_topic.status,sz_topic.group_id,sz_topic.createtime,sz_post.content,sz_topic.user_id,sz_user.cnname,sz_user.microblog,sz_image.url,sz_image.filename,sz_image.model 
		from sz_topic,sz_post,sz_user,sz_image 
		where sz_topic.id = $id AND sz_topic.status <>2 and sz_topic.user_id = sz_user.id and sz_user.image_id = sz_image.id and sz_post.topic_id=$id and sz_post.istopic=1
		");	
		
	}
	

	
	public function getPosts($topic)
	{
		return $this->query("
		SELECT sz_user.id as uid,sz_user.microblog,sz_user.cnname,sz_image.id,sz_image.url,sz_image.filename,sz_image.model,sz_post.content,sz_post.createtime,sz_post.id
		FROM sz_user,sz_image,sz_post
		WHERE sz_post.status<>2 and sz_post.user_id = sz_user.id and sz_user.image_id = sz_image.id and sz_post.istopic=0 and sz_post.topic_id = $topic
		");
	}
	
	public function getMyTopics($id)
	{
			
		return $this->query("
			SELECT sz_user.id as uid, sz_user.cnname , sz_group.id as gid, sz_group.title as groupname, sz_topic.id as tid, sz_topic.title,sz_topic.lastreplytime,sz_topic.postcount
			FROM sz_user, sz_group, sz_topic
			WHERE sz_topic.user_id = sz_user.id
			AND sz_topic.group_id = sz_group.id
			AND sz_topic.status = 1
			AND sz_topic.id in $id
			ORDER BY sz_topic.lastreplytime desc
			");
		
		
	}
	
	public function getManager($tid)
	{
		//adminids 扩展支持多个管理员
		$adminids = $this->query("
			SELECT sz_group.adminids  from sz_topic,sz_group where sz_topic.id = $tid and sz_topic.group_id = sz_group.id
		");
		return explode(',', $adminids[0]['adminids']);
	}
}

?>